package org.csu.mypetstore.service.impl;

import net.sf.json.JSONObject;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
//    AccountDao accountDao;
//
//    public AccountServiceImpl(){
//        this.accountDao = new AccountDaoImpl();
//    }

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account getAccountByUsernameAndPassword(String username, String password) {
        return accountMapper.getAccountByUsernameAndPassword(username, password);
    }

    @Override
    public Account getAccountByUsername(String username) {
        return  accountMapper.getAccountByUsername(username);
    }

    @Override
    public List<Account> getAccountList(){
        return accountMapper.getAccountList();
    }

    @Override
    public String addAccount(Account account) {
        String username1 = "";
        try {
            username1 = accountMapper.checkUsernameExists(account.getUsername());
        }catch(Exception e){
            e.printStackTrace();
        }
        if(username1!=null){
            //用户名已存在
            return "Username already exists";
        }
        try{
            accountMapper.addAccount(account);
            accountMapper.addSignon(account);
            return "Succeed in adding account and signon";
        }catch(Exception e) {
            e.printStackTrace();
            return "Fail to add";
        }
    }

    @Override
    public String updateAccount(Account account) {
        try{
            accountMapper.updateAccount(account);
            return "Succeed in updating account";
        }catch(Exception e) {
            e.printStackTrace();
            return "Fail to update account";
        }
    }

    @Override
    public String updateSignon(String username, String password) {
        if(password != null && !password.equals("")) {
            try{
                accountMapper.updateSignon(username, password);
                return "Succeed in updating signon";
            }catch(Exception e) {
                e.printStackTrace();
                return "Fail to update signon";
            }
        }
        return "Illegal password. Please try again.";
    }

    @Override
    public String deleteAccount(String username){
        String username1 = accountMapper.checkUsernameExists(username);
        if(username1==null){
            return "Username does not exist";
        }else{
            try {
                accountMapper.deleteAccount(username);
                accountMapper.deleteSignon(username);
                return "Succeed in deleting account and signon";
            }catch (Exception e){
                e.printStackTrace();
                return "Fail to delete account or signon";
            }

        }
    }

    @Override
    public boolean isAdmin(String username){
        return accountMapper.isAdmin(username);
    }
//
//    @Override
//    public JSONObject setJSON(Account account) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("username" )
//        return null;
//    }

}
